package entity

import (
	"fmt"
	"strings"
	"time"

	"gorm.io/gorm"
	"shqsoft.cn/pas3ui/base"
)

// Sgbj 手工补记
type Sgbj struct {
	ID   int       `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	DPNM string    `gorm:"column:DPNM"` //部门
	EMNO string    `gorm:"column:EMNO"` //工号
	EMNM string    `gorm:"column:EMNM"` //姓名
	BJXM string    `gorm:"column:BJXM"` //补记项目
	JE   float64   `gorm:"column:JE"`   //金额
	EMID int       `gorm:"column:EMID"` //员工id
	DPID int       `gorm:"column:DPID"` //部门id
	FFJD time.Time `gorm:"column:FFJD"` //发放季度
	DRID int       `gorm:"column:DRID"` //导入ID
}

// TableName
func (Sgbj) TableName() string {
	return "SH_JHCW_JXFF_SGBJ"
}

// BeforeSave
func (u *Sgbj) BeforeSave(tx *gorm.DB) (err error) {
	var emid, dpid, pdid *int
	var dpnm *string
	var emno, emnm string
	jdint := datenfjj(u.FFJD)
	err = can_import(tx, jdint)
	if err != nil {
		return
	}
	tx.Raw(`select "EMID","EMNM" from "SH_JHCW_JXFF_YGJX" where "FFJD"=? AND "EMNO" = ?`, jdint, u.EMNO).Row().Scan(&emid, &emnm)
	if emid == nil {
		err = tx.Raw(`select "DPID","DPNM","EMID","EMNO","EMNM","PDID" from "V_USER_DEPT" where "EMNO"=?`, u.EMNO).Row().Scan(&dpid, &dpnm, &emid, &emno, &emnm, &pdid)
		if err != nil || emid == nil {
			err = fmt.Errorf("员工号%s在类别绩效或绩效系统中不存在", u.EMNO)
			return
		}
		err = tx.Exec(`insert into "SH_JHCW_JXFF_YGJX" ("FFJD","DPID","DPNM","EMID","EMNO","EMNM","PDID") values(?,?,?,?,?,?,?)`, jdint, dpid, dpnm, emid, emno, emnm, pdid).Error
		if err != nil {
			return
		}
	}
	emnm_sys := strings.TrimSpace(emnm)
	emnm_imp := strings.TrimSpace(u.EMNM)
	if !strings.EqualFold(emnm_sys, emnm_imp) && !strings.Contains(emnm_sys, emnm_imp) {
		err = fmt.Errorf("员工号%s对应的姓名是%s,而提供姓名是%s", u.EMNO, emnm, u.EMNM)
		return
	}
	tx.Raw(`select "DPNM","DPID"  from "SH_JHCW_JXFF_YGJX" where "EMNO" = ? AND  "FFJD"=? `, u.EMNO, jdint).Row().Scan(&dpnm, &dpid)
	if dpid == nil {
		err = fmt.Errorf("部门%s在类别绩效中不存在,或者类别绩效没有导入", u.DPNM)
		return
	}
	u.DPID = *dpid
	u.DPNM = *dpnm
	u.EMID = *emid
	return
}

func datenfjj(d time.Time) int {
	year := d.Year()
	month := int(d.Month())
	day := d.Day()
	var jd, quarter int
	if day == 1 {
		quarter = year*100 + month
	} else {
		if month == 3 {
			jd = 1
			quarter = year*10 + jd
		} else if month == 6 {
			jd = 2
			quarter = year*10 + jd
		} else if month == 9 {
			jd = 3
			quarter = year*10 + jd
		} else if month == 12 {
			jd = 4
			quarter = year*10 + jd
		} else {
			quarter = year*100 + month
		}
	}
	return quarter
}

// SgbjAfterImport 导入后执行分户处理
func SgbjAfterImport(tx *gorm.DB, params map[string]string) (err error) {
	var (
		pwz, pmaxwz *int
		maxwz, wz   int
		sqlStr      string
	)
	ffjd := params["FFJD"]
	bjxm := params["BJXM"]
	tx.Raw(`select "SGBJWZPZB"  from "SH_JHCW_JXFF_SGBJ_WZPZB" sjjsw where "FFJD"  = ? and "XMMC" = ?`, ffjd, bjxm).Row().Scan(&pwz)
	if pwz == nil {
		tx.Raw(`select max("SGBJWZPZB")  from "SH_JHCW_JXFF_SGBJ_WZPZB" sjjsw where "FFJD"  = ?`, ffjd).Row().Scan(&pmaxwz)
		if pmaxwz != nil {
			maxwz = *pmaxwz
		}
		wz = maxwz + 1
		err = tx.Exec(`insert into "SH_JHCW_JXFF_SGBJ_WZPZB" ("SGBJWZPZB","XMMC","FFJD")values (?,?,?)`, wz, bjxm, ffjd).Error
		if err != nil {
			return
		}
	} else {
		err = tx.Raw(`select "SGBJWZPZB"  from "SH_JHCW_JXFF_SGBJ_WZPZB" sjjsw where "FFJD"  = ? and "XMMC" = ?`, ffjd, bjxm).Row().Scan(&maxwz)
		if err != nil {
			return
		}
		wz = maxwz
	}
	var date time.Time
	date, err = time.Parse("2006-01-02", ffjd)
	if err != nil {
		return
	}
	quarter := datenfjj(date)
	sqlstr := fmt.Sprintf(`UPDATE "SH_JHCW_JXFF_YGJX" SET "YWJX_SGBJ%d" = 0 WHERE "FFJD" = ?`, wz)
	err = tx.Exec(sqlstr, quarter).Error
	if err != nil {
		return
	}
	if base.AppConf.Database.Mysql != "" {
		sqlStr = fmt.Sprintf(`update "SH_JHCW_JXFF_YGJX" a,(select "EMNO",sum("JE") "JE" from "SH_JHCW_JXFF_SGBJ" where "BJXM" =? and "FFJD" = ? group by "EMNO") b  set a."YWJX_SGBJ%d" = b."JE"
		where a."EMNO" = b."EMNO" and a."FFJD" =?`, wz)
		err = tx.Exec(sqlStr, bjxm, ffjd, quarter).Error
	} else {
		sqlStr = fmt.Sprintf(`update "SH_JHCW_JXFF_YGJX" a set "YWJX_SGBJ%d" = b."JE"
		from (select "EMNO",sum("JE") "JE" from "SH_JHCW_JXFF_SGBJ" where "BJXM" =? and "FFJD" = ? group by "EMNO") b where a."EMNO" = b."EMNO" and a."FFJD" = ?`, wz)
		err = tx.Exec(sqlStr, bjxm, ffjd, quarter).Error
	}
	if err != nil {
		return
	}
	err = ImpJxffAfter(tx, quarter)
	return
}

// ShCompClieQygmEF 返回结构体
func SgbjEF() base.EntityFunc {
	return func() interface{} {
		entity := Sgbj{}
		return &entity
	}
}

// ShCompClieQygmESF 返回结构体数组
func SgbjESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []Sgbj
		return &entities
	}
}
func init() {
	base.DaoPoolObj.Put("Sgbj", base.NewDefaultDao(SgbjEF(), SgbjESF(), nil, "my"))
	base.SetAfterCallBack("Sgbj", SgbjAfterImport)
}
